package ch.elektroplan.christian.projectFinder.presentation;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ch.elektroplan.christian.projectFinder.business.ProjectFinder;

public class SetPositionFrame extends SubFrames {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private ProjectFinder projectFinder;
	private JFrame parent;
	private JButton cancel, save, actpos;
	private JTextField yCoordinate, xCoordinate;
	private JLabel tx_Xcoord, tx_yCoord;

	public SetPositionFrame(ProjectFinder projectFinder, JFrame parent) {
		this.projectFinder = projectFinder;
		this.parent = parent;

		this.setLocation(parent.getLocation());
		this.setSize(400, 150);
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		this.parent.setEnabled(false);

		this.tx_Xcoord = new JLabel("X-Coordinate");
		this.tx_yCoord = new JLabel("Y-Coordinate");
		
		this.xCoordinate = new JTextField();
		this.xCoordinate.setPreferredSize(new Dimension(150, 20));
		this.xCoordinate.setEnabled(false);
		
		this.yCoordinate = new JTextField();
		this.yCoordinate.setPreferredSize(new Dimension(150, 20));
		this.yCoordinate.setEnabled(false);
		
		this.save = new JButton("save");
		this.save.addActionListener(new ButtonActionListener());

		this.cancel = new JButton("cancel");
		this.cancel.addActionListener(new ButtonActionListener());

		this.actpos = new JButton("current position");
		this.actpos.addActionListener(new ButtonActionListener());

		this.add(new ButtonPanel(), BorderLayout.SOUTH);
		this.add(new InputField(), BorderLayout.CENTER);

		this.setVisible(true);
	}

	private class InputField extends JPanel {

		public InputField() {

			add(new TexfieldPanel(), BorderLayout.CENTER);
			add(actpos, BorderLayout.EAST);

		}
	}

	private class ButtonPanel extends JPanel {

		public ButtonPanel() {

			setLayout(new FlowLayout());
			add(cancel);
			add(save);

		}
	}

	private class ButtonActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource().equals(cancel)) {

				closeAndrewake(parent);
			} else if (e.getSource().equals(actpos)) {
				actposAction();
			} else if (e.getSource().equals(save)) {

				if (isInteger(xCoordinate.getText())
						&& isInteger(yCoordinate.getText())) {
					save(projectFinder,
							Integer.parseInt(xCoordinate.getText()),
							Integer.parseInt(yCoordinate.getText()));
					closeAndrewake(parent);
				}else{
					JOptionPane.showMessageDialog(SetPositionFrame.this, "No valid input");
				}

			}

		}

		private void actposAction() {

			xCoordinate.setText("" + (int) parent.getLocation().getX());
			yCoordinate.setText("" + (int) parent.getLocation().getY());

		}

	}

	private class TexfieldPanel extends JPanel {

		public TexfieldPanel() {
			add(new TexFieldContentPanel(tx_Xcoord, tx_yCoord),
					BorderLayout.WEST);
			add(new TexFieldContentPanel(xCoordinate, yCoordinate),
					BorderLayout.CENTER);
		}

	}

	private class TexFieldContentPanel extends JPanel {

		public TexFieldContentPanel(Component c1, Component c2) {
			setLayout(new GridLayout(2, 1));
			add(c1);
			add(c2);
		}
	}

	public boolean isInteger(String text) {

		try {
			Integer.parseInt(text);
		} catch (NumberFormatException e) {
			return false;
		}
		return true;
	}
}
